﻿
function goBack() {
    location.href = '/Role/RoleList';
}
var $checkableTree;
var pagesCheck = new Array();

function SubData() {
    var postData = {
        RoleID: $("#hid").val(),
        RoleName: $("#Rolename").val(),
        IsActive: $("#PIsActive").val(),
        CreateTime: $("#hidCreateTime").val(),
        CreateUserID: $('#hidCreateUserID').val(),
        Remark: HtmlUtil.htmlEncodeByRegExp(CKEDITOR.instances.desTextarea.getData()),
        PagesCheck: pagesCheck
    };
    $.ajax({
        type: 'post',
        traditional: true,
        url: '/Role/Updateinfo',
        data: postData,
        success: function (data) {
            if (data == "suc") {
                //关闭层，刷新表单
                goBack();
            }
            else if (data == "repeat") {
                $.alert({
                    title: '系统提示',
                    content: '重复数据无法添加！',
                    confirm: function () {
                    }
                });
            }
            else {
                $.alert({
                    title: '系统提示',
                    content: '重复数据无法添加！',
                    confirm: function () {
                    }
                });
            }
        },
        error: function () {
            $.alert({
                title: '系统提示',
                content: '操作失败！',
                confirm: function () {
                }
            });
        }
    });
}


$(document).ready(function () {
    //初始化左侧菜单
    $("#rightUser").addClass("active");
    CKEDITOR.replace('desTextarea', {
        Rolebar: 'Basic'
        //uiColor: '#9AB8F3'//界面颜色
    });
    CKEDITOR.instances.desTextarea.setData(HtmlUtil.htmlDecodeByRegExp($("#hidDescription").val()));
    var menulist = $("#hidmenu").val();
    if (menulist != "") {
        var mlist = menulist.split("|");
        for (var m in mlist) {
            pagesCheck.push(mlist[m]);
        }
    }
    $.ajax({
        type: "post",
        url: "/SysMenu/GetMenuNodebyRole",
        data: { id: $("#hid").val() },
        dataType: "json",
        success: function (data) {
            data = eval(data);

            $checkableTree = $('#treeview-checkable').treeview({
                data: data,
                showIcon: false,
                showCheckbox: true,
                onNodeChecked: function (event, node) {
                    pagesCheck.push(node.id);
                },
                onNodeUnchecked: function (event, node) {
                    var index = pagesCheck.indexOf(node.id);
                    if (index > -1) {
                        pagesCheck.splice(index, 1);
                    }

                }
            });
        }

    });

    $('#RoleUpdateForm')
    .bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            Rolename: {
                message: 'The Role Name is not valid',
                validators: {
                    notEmpty: {
                        message: 'The Role Name is required and can\'t be empty'
                    }
                }
            }
        }
    })
    .on('success.form.bv', function (e) {
        e.preventDefault();
        SubData();
    });
});
